//
//  ZJMapNavigationViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/14.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJMapNavigationViewController.h"
#import "XLSlideMenu.h"
#import "LPSearchViewController.h"
#import "QRCodeReaderViewController.h"
#import "LPSignSuccessShowView.h"//扫一扫
#import "ZJTodaySingWebViewController.h"//签到
#import "YBRequestManager.h"
#import "ZJBBranchViewController.h"
//#import "NetWorkModel.h"
@interface ZJMapNavigationViewController ()<YBPopupMenuDelegate>
//加号小红点
@property (nonatomic , strong) UIImageView * rightBadgeView;
//icon图标数组
@property (nonatomic , strong) NSArray * iconArray;
@end

@implementation ZJMapNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentSignStatus = YES;
    self.iconArray = @[@"more_saoyisao",@"more_qiandao"];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    [self.navView addSubview:self.statusVeiw];
    [self.view addSubview:self.navView];
//    [self.navView addSubview:self.rightBadgeView];
}




-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //刷新红点状态
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"YBIsSign"] integerValue] == 1) {
        [self reloadSignStatusWithStatus:YES];
        self.rightBadgeView.hidden = YES;
    }
    else{
        [self reloadSignStatusWithStatus:NO];
        self.rightBadgeView.hidden = NO;
    }
    if (!self.isMyBranchViewController) {
        [self refreshSignStatusRequest];
    }
}



-(void)refreshSignStatusRequest{
    [YBRequestManager request_partyMemberHomePageListWithAccountId:ZJUSERINFORMODEL.ORG_ACCOUNT.ID partyMemberId:ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID unitId:ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSString * isSignString = [NSString stringWithFormat:@"%@" , object[@"SIGN"][@"SIGN_STATE"]];
            if ([isSignString isEqualToString:@"已签到"]) {
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"YBIsSign"];
                [self reloadSignStatusWithStatus:YES];
            }
            else{
                [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"YBIsSign"];
                [self reloadSignStatusWithStatus:NO];
            }
        }
    } failBlock:^(id object) {
        
    }];
}

-(void)reloadSignStatusWithStatus:(BOOL)status{
    //如果红点状态没有改变，则没有任何操作
    if ((int)self.currentSignStatus == (int)status) {
        return;
    }
    self.currentSignStatus = status;
    if (status) {
        //已签到
        self.iconArray = @[@"more_saoyisao",@"more_qiandao"];
        self.rightBadgeView.hidden = YES;
    }
    else{
        //未签到
        self.iconArray = @[@"more_saoyisao",@"more_qiandao_news"];
        self.rightBadgeView.hidden = NO;
    }
}


#pragma mark - 点击事件
- (void)action_leftButtonClick{
    ZJBBranchViewController *homeVc = [[ZJBBranchViewController alloc] init];
    homeVc.type = EBranchType;
    [[ToolClass getMainNavigationController] pushViewController:homeVc animated:YES];
}


- (void)action_searchClick{
    LPSearchViewController *vc = [LPSearchViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)search_Click{
    LPSearchViewController *vc = [LPSearchViewController new];
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)action_rightButtonClick:(UITapGestureRecognizer *)sender{
   self.menu = [YBPopupMenu showRelyOnView:self.navView.rightButton titles:@[@"扫一扫",@"签到"] icons:self.iconArray menuWidth:self.view.width * 0.34 showCount:0 delegate:self];
    self.menu.fontSize = 14 * [UIScreen zj_textFontScale];
}


#pragma mark - YBPopupMenuDelegate 协议方法
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    if (index == 0) {//扫一扫
        /**  这是签到  */
        
        QRCodeReaderViewController *reader = [QRCodeReaderViewController new];
        
        reader.push = NO;
        reader.modalPresentationStyle = UIModalPresentationFormSheet;
        [reader setCompletionWithBlock:^(NSString *resultAsString) {
            
        } login:^(BOOL successOrFail) {
            
        } sign:^(NSDictionary *successOrFail) {
            if ([successOrFail[@"STATE"] isEqualToString:@"20000"]) {
                LPSignSuccessShowView *sign = [[LPSignSuccessShowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
                NSString *point = successOrFail[@"growUp"][@"point"];
                sign.data = @[point];
                if ([point integerValue] != 0) {
                    [[UIApplication sharedApplication].keyWindow addSubview:sign];
                }
            }
        }];
        [self.navigationController pushViewController:reader animated:YES];
    }else{//签到
        ZJTodaySingWebViewController *vc = [ZJTodaySingWebViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - Getters

- (ZJMapNavControllView *)navView{
    if (_navView == nil){
        _navView = [[ZJMapNavControllView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, LP_TopHeight) leftImage:@"img_touxiang" rightImage:@"home_btn_more"];
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_leftButtonClick)];
        [_navView.leftButton addGestureRecognizer:tap1];
        [_navView.rightButton addTarget:self action:@selector(action_rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_navView  setNoAddButton];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(search_Click)];
        [_navView.searchLabel addGestureRecognizer:tap];
    }
    return _navView;
}

- (UIView *)statusVeiw{
    if (!_statusVeiw) {
        _statusVeiw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 20)];
        _statusVeiw.backgroundColor = [UIColor themeColor];
    }
    return _statusVeiw;
}


-(UIImageView *)rightBadgeView
{
    if (!_rightBadgeView) {
        _rightBadgeView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen zj_screenWith] - 17 * [UIScreen zj_screenWScale] , 26, 11 * [UIScreen zj_screenWScale], 11 * [UIScreen zj_screenWScale])];
        _rightBadgeView.image = [UIImage imageNamed:@"icon_circle"];
        _rightBadgeView.hidden = YES;
    }
    return _rightBadgeView;
}


@end
